package com.example.pc.mytestapplication.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;

import com.example.pc.mytestapplication.adapter.AdapterMain;
import com.example.pc.mytestapplication.data.MainData;
import com.example.pc.mytestapplication.R;
import com.example.pc.mytestapplication.databinding.ActivityMainBinding;
import com.example.pc.mytestapplication.test.TestMainActivity;
import com.example.pc.mytestapplication.util.TitleBarUtils;
import com.example.pc.mytestapplication.util.YZLog;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class MainActivity extends BaseActivity {


    private ActivityMainBinding binding;
    private List<MainData> mainData;
    private Observable<Integer> observable;
    private AdapterMain adapterMain;
    private Intent i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        TitleBarUtils.setTitle(MainActivity.this, "标准操作符", false);
        binding.btMain.setOnClickListener(this);
        binding.btDemo.setOnClickListener(this);
        mainData = new ArrayList<>();
        mainData.add(new MainData("标准", "尚未计算"));
        mainData.add(new MainData("使用just快速创建Observable并按顺序发送", "尚未计算"));
        mainData.add(new MainData("使用fromArray快速创建Observable并按顺序发送", "尚未计算"));
        mainData.add(new MainData("使用fromInterable快速创建Observable并按顺序发送", "尚未计算"));
        mainData.add(new MainData("使用empty直接调用onComplete方法", "尚未计算"));
        mainData.add(new MainData("使用error直接调用onError方法", "尚未计算"));
        mainData.add(new MainData("使用never不调用观察者方法", "尚未计算"));
        mainData.add(new MainData("使用defer动态调用观察者", "尚未计算"));
        mainData.add(new MainData("使用tiem延时处理数据", "尚未计算"));
        mainData.add(new MainData("使用interval无限轮训数据", "尚未计算"));
        mainData.add(new MainData("使用intervalRande有限轮训数据", "尚未计算"));
        mainData.add(new MainData("使用range轮训指定次数数据", "尚未计算"));
        adapterMain = new AdapterMain(this, mainData);
        binding.lvMain.setAdapter(adapterMain);

        observable = Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(10010);
                emitter.onNext(10086);
                emitter.onError(new RuntimeException());
                emitter.onNext(10011);
                emitter.onComplete();
            }
        });

        binding.lvMain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                YZLog.i("当前ListVew中的第" + i + "条条目被点击");
                switch (i) {
                    case 0:
                        Observer<Integer> observer = new Observer<Integer>() {
                            private int sum;

                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(Integer integer) {
                                sum = sum + integer;
                            }

                            @Override
                            public void onError(Throwable e) {
                                YZLog.e("异常!" + e.getClass().getSimpleName());
                            }

                            @Override
                            public void onComplete() {
                                mainData.get(i).setTextType("计算结果为:" + sum);
                                adapterMain.notifyDataSetChanged();
                                sum = 0;
                            }
                        };
                        observable.subscribe(observer);
                        break;
                    case 1:

                        Observable.just(10086, 10010, 10011).subscribe(new Observer<Integer>() {
                            private int sum;

                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(Integer integer) {
                                sum += integer;
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {
                                mainData.get(i).setTextType("使用just快速创建并链式调用的计算结果为:" + sum);
                                adapterMain.notifyDataSetChanged();
                                sum = 0;
                            }
                        });
                        break;
                    case 2:
                        Integer[] intArray = {10086, 10011, 10010};
                        Observable.fromArray(intArray).subscribe(new Observer<Integer>() {
                            private int sum;

                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(Integer integer) {
                                sum += integer;
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {
                                mainData.get(i).setTextType("使用FromArray快速创建并链式调用的计算结果为:" + sum);
                                adapterMain.notifyDataSetChanged();
                                sum = 0;
                            }
                        });
                        break;
                    case 3:
                        ArrayList<Integer> integers = new ArrayList<>();
                        integers.add(10086);
                        integers.add(10010);
                        integers.add(10011);
                        Observable.fromIterable(integers).subscribe(new Observer<Integer>() {
                            private int sum;

                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(Integer integer) {
                                sum += integer;
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {
                                mainData.get(i).setTextType("使用FromInterable快速创建并链式调用的计算结果为:" + sum);
                                adapterMain.notifyDataSetChanged();
                                sum = 0;
                            }
                        });
                        break;
                    case 4:
                        Observable.empty().subscribe(new Observer<Object>() {
                            private StringBuffer info = new StringBuffer();

                            @Override
                            public void onSubscribe(Disposable d) {
                                info.append("onSubscribe");
                            }

                            @Override
                            public void onNext(Object o) {
                                info.append("\\onNext");
                            }

                            @Override
                            public void onError(Throwable e) {
                                info.append("\\onError");

                            }

                            @Override
                            public void onComplete() {
                                info.append("\\onComplete");

                                mainData.get(i).setTextType("使用empty直接调用onComplete方法的执行顺序:" + info.toString());
                                adapterMain.notifyDataSetChanged();
                            }
                        });
                        break;
                    case 5:
                        Observable.error(new RuntimeException()).subscribe(new Observer<Object>() {
                            private StringBuffer info = new StringBuffer();

                            @Override
                            public void onSubscribe(Disposable d) {
                                info.append("onSubscribe");
                            }

                            @Override
                            public void onNext(Object o) {
                                info.append("\\onNext");
                            }

                            @Override
                            public void onError(Throwable e) {
                                info.append(" \\onError:" + e.toString());
                                mainData.get(i).setTextType("使用error方法直接调用onError的执行顺序为:" + info.toString());
                                adapterMain.notifyDataSetChanged();
                            }

                            @Override
                            public void onComplete() {
                                info.append("\\onComplete");
                                info.append("我如果被执行就说明即使出现了Error也会调用onComplete方法");
                                mainData.get(i).setTextType("使用error方法直接调用onError的执行顺序为:" + info.toString());
                                adapterMain.notifyDataSetChanged();
                            }
                        });
                        break;
                    case 6:
                        Observable.never().subscribe(new Observer<Object>() {
                            private StringBuffer info = new StringBuffer();

                            @Override
                            public void onSubscribe(Disposable d) {
                                info.append("onSubscribe");
                                mainData.get(i).setTextType("使用never方法直接调用的执行顺序为:" + info.toString());
                                adapterMain.notifyDataSetChanged();
                            }

                            @Override
                            public void onNext(Object o) {
                                info.append("\\onNext");
                                mainData.get(i).setTextType("使用never方法直接调用的执行顺序为:" + info.toString());
                                adapterMain.notifyDataSetChanged();
                            }

                            @Override
                            public void onError(Throwable e) {
                                info.append(" \\onError:" + e.toString());
                                mainData.get(i).setTextType("使用never方法直接调用的执行顺序为:" + info.toString());
                                adapterMain.notifyDataSetChanged();
                            }

                            @Override
                            public void onComplete() {
                                info.append("\\onComplete");
                                info.append("我如果被执行就说明即使出现了Error也会调用onComplete方法");
                                mainData.get(i).setTextType("使用never方法直接调用的执行顺序为:" + info.toString());
                                adapterMain.notifyDataSetChanged();
                            }
                        });
                        break;
                    case 7:
                        final List<Integer> list = new ArrayList<>();
                        list.add(10086);
                        list.add(10010);
                        list.add(10011);
                        Observable<Integer> observable = Observable.defer(new Callable<ObservableSource<? extends Integer>>() {
                            @Override
                            public ObservableSource<? extends Integer> call() throws Exception {
                                return Observable.fromIterable(list);
                            }
                        });
                        Observable<List<Integer>> integerObservable = Observable.create(new ObservableOnSubscribe<List<Integer>>() {
                            @Override
                            public void subscribe(ObservableEmitter<List<Integer>> emitter) throws Exception {
                                emitter.onNext(list);
                                emitter.onComplete();
                            }
                        });
                        for (int j = 0; j < list.size(); j++) {
                            list.set(j, 1);
                        }
                        integerObservable.subscribe(new Observer<List<Integer>>() {
                            private int sum;

                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(List<Integer> integers) {
                                for (int i = 0; i < integers.size(); i++) {
                                    sum += integers.get(i);
                                }
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {
                                mainData.get(i).setTextType("直接处理的数据:" + sum);
                            }
                        });
                        observable.subscribe(new Observer<Integer>() {
                            private int sum;

                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(Integer integer) {
                                sum += integer;
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {
                                mainData.get(i).setTextType(mainData.get(i).getTextType() + "使用defer方法动态订阅后处理的数据:" + sum);
                                adapterMain.notifyDataSetChanged();
                            }
                        });
                        break;
                    case 8:
                        mainData.get(i).setTextType("使用timer方法已经开始延时处理数据");
                        adapterMain.notifyDataSetChanged();

                        Observable.timer(2, TimeUnit.SECONDS).subscribe(new Observer<Long>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(Long aLong) {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {
                                YZLog.i("数据延时处理完成");
                                mainData.get(i).setTextType("使用timer方法处理完数据");
                                adapterMain.notifyDataSetChanged();
                            }
                        });
                        break;
                    case 9:
                        Observable.interval(2, 1, TimeUnit.SECONDS)
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Observer<Long>() {

                                    private Disposable d;

                                    @Override
                                    public void onSubscribe(Disposable d) {
                                        this.d = d;
                                    }

                                    @Override
                                    public void onNext(Long aLong) {
                                        mainData.get(i).setTextType("使用interval方法无线轮训数据" + aLong);
                                        adapterMain.notifyDataSetChanged();
                                        if (aLong > 5) {
                                            d.dispose();
                                            mainData.get(i).setTextType("轮训指定次数后断开连接" + aLong);
                                            adapterMain.notifyDataSetChanged();
                                        }
                                    }

                                    @Override
                                    public void onError(Throwable e) {

                                    }

                                    @Override
                                    public void onComplete() {

                                    }
                                });
                        break;
                    case 10:
                        Observable.intervalRange(1, 8, 1, 1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(Long aLong) {
                                mainData.get(i).setTextType("使用intervalRange方法开始有限轮训" + aLong);
                                adapterMain.notifyDataSetChanged();
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {
                                mainData.get(i).setTextType("使用intervalRange方法轮序到一定次数后结束");
                                adapterMain.notifyDataSetChanged();
                            }
                        });
                        break;
                    case 11:
                        Observable.range(1, 10).subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Integer>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(Integer integer) {
                                mainData.get(i).setTextType("使用range方法无间隔轮序指定次数的事件" + integer);
                                adapterMain.notifyDataSetChanged();
                                YZLog.e("使用range方法无间隔轮序指定次数的事件" + integer);
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {
                                mainData.get(i).setTextType("使用range方法轮训结束");
                                YZLog.e("使用range方法无间隔轮训结束");
                                adapterMain.notifyDataSetChanged();
                            }
                        });
                        break;
                }
            }
        });
    }

    @Override
    protected void setinitView() {

    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void loadView() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_main:
/*                DiffUtil.DiffResult diffResult =
                        DiffUtil.calculateDiff(new DiffCallBack(oldDatas, newDatas), true);
                diffResult.dispatchUpdatesTo(adapterMain);*/
                i = new Intent(MainActivity.this, ConversionActivity.class);
                startActivity(i);
                break;
            case R.id.bt_demo:
                i = new Intent(MainActivity.this, TestMainActivity.class);
                startActivity(i);
                break;

        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
